package gr.teiath.newsfeed;

import java.util.ArrayList;
import java.util.Arrays;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class FacultyAdapter extends SimpleCursorAdapter implements OnClickListener {
	
	private static final String tag = FacultyAdapter.class.getSimpleName();
	private static final String PREF_NAME = "facultyPrefs";
	private Context context;
	private Cursor cursor;
	private int layout;
	private ArrayList<String> selected = new ArrayList<String>(); 
	private NewsfeedApplication newsfeed;

	public FacultyAdapter(Context context, int layout, Cursor c, String[] from,int[] to) {
		super(context, layout, c, from, to);
		newsfeed = new NewsfeedApplication(); // correct?
		
		this.context = context;
		this.cursor = c;
		this.layout = layout;
		
		selected = newsfeed.LoadFaculties(this.context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = new ViewHolder();
		cursor.moveToPosition(position);
		String facultyId = cursor.getString(cursor.getColumnIndex("_id"));
		int parse_type = Integer.parseInt(cursor.getString(cursor.getColumnIndex("p_type")));
		
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(layout, null);

			holder.tView = (TextView) convertView.findViewById(R.id.facultyTxtView);
			holder.cBox = (CheckBox) convertView.findViewById(R.id.facultyChkBox); 
			holder.alertIcon = (ImageView) convertView.findViewById(R.id.alertIcon); 
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.tView.setText(cursor.getString(cursor.getColumnIndex("name")));
		
		if(parse_type > 0) {
			holder.cBox.setTag(facultyId);
			holder.cBox.setChecked(this.selected.contains(facultyId));
			holder.cBox.setOnClickListener(this);
		} else {
			holder.cBox.setVisibility(View.GONE);
			holder.alertIcon.setVisibility(1);
			holder.alertIcon.setOnClickListener(this);
		}

		return convertView;
	}
	
	//@Override
	public void onClick(View view) {
		if(view.getTag() != null) {
			CheckBox cBox = (CheckBox) view;
			String facultyId = (String) cBox.getTag();
			Log.i(tag, "****** FacultyAdapter // onClick");
			if (cBox.isChecked()) {
				if (!this.selected.contains(facultyId)) this.selected.add(facultyId);
			} else {
				if (this.selected.contains(facultyId)) this.selected.remove(facultyId);
			}
			this.SaveFaculties();
		} else {
			Toast.makeText(this.context, this.context.getText(R.string.facultyUnavailable), Toast.LENGTH_LONG).show();
		}
	}
	
	public void SaveFaculties() {
		String selectedString = Arrays.toString(this.selected.toArray());
		selectedString = selectedString.substring(1, selectedString.length()-1); // remove "[" & "]" characters
		selectedString = selectedString.replace(" ",""); // remove whitespace
		
		SharedPreferences facultyPrefs = PreferenceManager.getDefaultSharedPreferences(this.context);

		SharedPreferences.Editor prefEditor = facultyPrefs.edit();
		Log.i(tag, "****** FacultyAdapter // Saving facultyPrefs string:" + selectedString);
		prefEditor.putString(PREF_NAME, selectedString);
		
		prefEditor.commit();
	}
	
	static class ViewHolder {
		public TextView tView;
		public CheckBox cBox;
		public ImageView alertIcon;
	}

}